Skip to content

Vulkan: wrap external buffers as regions#9110

Open
xFile3160 wants to merge 2 commits intohalide:mainfrom
xFile3160:vulkan-import-buffer-offsets
Open

Vulkan: wrap external buffers as regions#9110
xFile3160 wants to merge 2 commits intohalide:mainfrom
xFile3160:vulkan-import-buffer-offsets

Conversation

@xFile3160
Copy link
Copy Markdown

@xFile3160 xFile3160 commented Apr 25, 2026

This fixes the Vulkan native-buffer wrapper path.

The issue in #8715 started from trying to pass an application-owned VkBuffer
to Halide. The only way to make that work reliably was to mirror Halide's
private MemoryRegion layout in application code and pass that through
halide_vulkan_wrap_vk_buffer. That is not a good API boundary.

halide_vulkan_wrap_vk_buffer should accept a Vulkan buffer handle and let
Halide build whatever runtime metadata it needs internally.

This PR changes the wrapper path to do that. External VkBuffer handles are
wrapped in small runtime-owned MemoryRegion metadata. The Vulkan buffer
remains owned by the caller; Halide only owns the metadata needed to pass the
buffer through descriptor updates, copies, crops, and detach/free.

The intended flow is:

  1. The application allocates and owns a VkBuffer.
  2. The application wraps it into a halide_buffer_t.
  3. Halide creates its own metadata for the wrapped buffer.
  4. Halide uses that metadata internally.
  5. Detach/free releases only Halide's wrapper metadata, not the external
    VkBuffer.

This also adds halide_vulkan_wrap_vk_buffer_with_offset for views into larger
Vulkan buffers. The explicit external offset is included in descriptor updates
and copy paths. Regular Halide crops continue to use the existing indexing
offset path.

The GPU object lifetime test now covers Vulkan native buffer wrapping. It
rewraps a Vulkan buffer handle, then wraps the same buffer with an explicit byte
offset and verifies that the shifted view copies back the expected values.

Refs #8715

@alexreinking
Copy link
Copy Markdown
Member

This description sounds written by AI. Per our contribution guidelines, please disclose any AI tool usage or assert that none was used.

@xFile3160
Copy link
Copy Markdown
Author

Indeed. I did not review yet. Please don't until I do and I update description. Those things work on their own now

@alexreinking alexreinking marked this pull request as draft April 27, 2026 17:43
@alexreinking
Copy link
Copy Markdown
Member

@xFile3160 -- when ready for review, change the status away from Draft.

halide_vulkan_wrap_vk_buffer stored the raw VkBuffer in halide_buffer_t::device, but the Vulkan runtime expects that field to point at MemoryRegion metadata.

Wrap external VkBuffer handles in small runtime-owned MemoryRegion metadata instead. Detach/free release only that metadata, not the external buffer. Add an explicit-offset wrap helper for views into larger buffers, and include that offset in descriptor updates and copy paths.

Refs halide#8715
@xFile3160 xFile3160 force-pushed the vulkan-import-buffer-offsets branch from 1398255 to d7ca1b1 Compare April 28, 2026 19:27
@xFile3160 xFile3160 marked this pull request as ready for review April 28, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants